package encaps.action;

import encaps.entities.Grant;
import encaps.entities.Role;
import encaps.resources.Grants;
import encaps.resources.Image;
import encaps.resources.Labels;
import encaps.resources.Response;
import encaps.security.RoleManager;
import encaps.security.SecuritySystem;
import encaps.utils.DataAccessException;
import encaps.utils.SystemException;
import encaps.utils.XMLHelper;
import java.util.Collection;
import java.util.Iterator;
import org.jdom.Element;

public class ViewRoleAction extends Action {

    private ActionContext actionContext;
    public static final String VIEW_ROLE = "viewRole";
    public static final String ROLE = "Role";

    public ViewRoleAction(ActionContext context, Grant requiredGrant) {
        this.actionContext = context;
        this.requiredGrant = requiredGrant;
    }

    @Override
    public Element execute() throws ActionExecutionException {
        try {
            Role vrole = SecuritySystem.getRole(actionContext.getTextParameter(ID));

            Element resultXML = XMLHelper.createElement(Response.RESPONSE);
            Element e = XMLHelper.addChild(resultXML, Response.TABLE);
            //Создаем заголовок страницы
            XMLHelper.addChild(e, Response.HEADER, "Role '" + vrole.getRoleName() + "' grant list:");
            //Создаем заголовки столбцов таблиц
            Element headers = XMLHelper.createElement(Response.COLUMN_HEADERS);
            XMLHelper.addChild(headers, Response.COLUMN_HEADER, Labels.getLabel(Labels.GRANTS));
            XMLHelper.addChild(e, headers);
            Collection<Grant> grants = vrole.getGrants();
            //Заполняем таблицу
            //Заполняем информацию, необходимую для пагинации
            Element rows = XMLHelper.createElement(Response.ROWS);
            for (Iterator<Grant> it = grants.iterator(); it.hasNext();) {
                Grant grant = it.next();
                Element row = XMLHelper.addChild(rows, Response.ROW);
                Element cell = XMLHelper.addChild(row, Response.CELL, grant.getGrantName());
                cell.setAttribute(Response.TYPE, Response.TEXT);

            }
            XMLHelper.addChild(e, rows);
            System.out.println("ROLE: " + vrole.getRoleName());
            if (!vrole.getRoleName().equals(Role.ADMIN)
                    && !vrole.getRoleName().equals(Role.USER)
                    && !vrole.getRoleName().equals(Role.GUEST)
                    && !vrole.getRoleName().equals(Role.MODERATOR)) {
                //System.out.println("ROLE: ");
                Element buttons = XMLHelper.addChild(resultXML, Response.ENTITY);
                buttons.setAttribute(Response.EDIT, "true");
                Role userRole = RoleManager.getRoleManager().getRole(actionContext.getUserRole());
                if (userRole.hasGrant(Grants.EDIT_ROLE)) {
                    XMLHelper.addChild(buttons, XMLHelper.createImageButton(Image.getImage(Image.EDIT), EditRoleAction.EDIT_ROLE));
                }
                if (userRole.hasGrant(Grants.DELETE_ROLE)) {
                    XMLHelper.addChild(buttons, XMLHelper.createImageButton(Image.getImage(Image.DELETE), DeleteRoleAction.DELETE_ROLE));
                }
                XMLHelper.addChild(buttons, XMLHelper.createHiddenTextField(vrole.getRoleName(), ID));
                //XMLHelper.addChild(resultXML, buttons);
            }
            return resultXML;
        } catch (DataAccessException ex) {
            throw new ActionExecutionException("Data access exception", ex);
        } catch (SystemException ex) {
            throw new ActionExecutionException("System malfunction", ex);
        }

    }
}